<?php
/**
 * 管理员表内置数据
 */
namespace BaseBundle\DataFixtures\ORM;

use BaseBundle\Entity\Admin;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class LoadAdminData implements FixtureInterface, ContainerAwareInterface
{

    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * {@inheritdoc}
     */
    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }
    
    /**
     * Load data fixtures with the passed EntityManager
     *
     * @param ObjectManager $manager
     */
    public function load(ObjectManager $manager)
    {
        $utilsStrings = $this->container->get('services.strings');
        $conn = $this->container->get('database_connection');

        $data = $this->container->get('services.files')->readToJson('app' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'admin.json');
        foreach ($data as $value){
            $value['created_at'] = date('Y-m-d H:i:s');
            $value['password'] = $utilsStrings->encryptPassword($value['password']);
            $value['realname'] = $value['username'];
            $conn->insert('admin', $value);
        }
    }

}